package com.jic.market.task;

import com.jic.market.entity.coupon.CouponDetail;
import com.jic.market.mapper.coupon.CouponDetailMapper;
import com.jic.member.api.rest.MemberInformationApi;
import com.jic.member.request.MeinformationRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Configuration
@EnableScheduling
public class CouponTasks {

    @Resource
    private CouponDetailMapper couponDetailMapper;
    @Resource
    private MemberInformationApi memberInformationApi;

    @Scheduled(cron = "0 0 8 * * ?")
    private void couponExpiredNoticeTask(){
        //查询结束时间小于等于7天优惠券
        List<CouponDetail> couponDetailList = couponDetailMapper.queryCouponExpired();
        couponDetailList.forEach(couponDetail -> {
            memberInformationApi.insertMemberInformation(new MeinformationRequest(){{
                setMemberId(Long.valueOf(couponDetail.getThirdUserId()));
                setCategory(2);
                setIsRead(2);
                setTitle("您有尚未使用优惠券即将到期，请尽快使用");
                setType(6);
            }});
        });
        List<Long> couponIds = couponDetailList.stream().map(CouponDetail::getId).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(couponIds)){
            log.info("update --- coupon --- ids:{}",couponIds);
            //批量更新通知状态
            couponDetailMapper.bacthCouponExpiredStatus(couponIds,2);
        }
    }

}
